<?php
namespace app\index\service;


use app\index\model\CertUseListModel;
use app\index\model\ContractBianModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractNeiModel;
use app\index\model\ContractShiModel;
use app\index\model\IncomeEnginModel;
use app\index\model\PiaokaiModel;

class ContractService extends CommonService
{

    public function get_contract_nei_state(){
        return [
            0=>'未签订',
            1=>'已签订',
        ];
    }
    public function get_contract_nei_state_name($contractid, $state){
        $name = '';
        if($state == 0){
            $name = '未签订';
        }
        if($state == 1){
            $name = '已签订';
        }
        return $name;
    }
    public function get_contract_nei_state_name_tag($contractid, $state){
        $name = '';
        if($state == 0){
            $name = '<span class="mindiancheng"></span>未签订';
        }
        if($state == 1){
            $name = '<span class="mindianlv"></span>已签订';
        }
        return $name;
    }



    public function get_contract_nei_feitype(){
        return [
            1=>'按固定金额',   //计算值
            2=>'按月固定金额',  //计算值 * 月份数   2022-01  2022-02  算两个月
            3=>'按合同金额比例',//计算值 * 合同金额（施工合同最终金额）
            4=>'按工程收入比例',//计算值 * 项目收款（款项类型是工程款的金额合计）
            5=>'按开票金额比例',//计算值 * 项目开票金额合计
            6=>'按用证清单比例',
        ];
    }
    public function get_contract_nei_feitype_name($feitype){
        $str = '';
        $arr = $this->get_contract_nei_feitype();
        $str = isset($arr[$feitype]) ? $arr[$feitype] : '';
        return $str;
    }
    public function get_contract_nei_feivalue($data, $enginid){
        $fee = 0;
        if($data['feitype'] == 1){
            $fee = $data['feival'];
        }
        if($data['feitype'] == 2){
            $months = $this->get_nei_months($data['startdt'], $data['enddt']);
            $fee = round($data['feival'] * $months, 2);
        }
        if($data['feitype'] == 3){
            $hemoney = (new EnginService())->get_contract_shi_final_money($enginid);
            $fee = round($data['feival'] * $hemoney, 2);
        }
        if($data['feitype'] == 4){
            $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
            $budshou = (new BudgetService())->get_budshou_sons($gckid);
            $gongkuan = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['enginid'=>$enginid])->where('budid','in',$budshou)->sum('money');
            $fee = round($data['feival'] * $gongkuan, 2);
        }
        if($data['feitype'] == 5){
            $kaimoney = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1,'enginid'=>$enginid])->sum('money');
            $fee = round($data['feival'] * $kaimoney, 2);
        }
        if($data['feitype'] == 6){
            $certfee = 0;
            $list = CertUseListModel::alias('list')
                ->field('list.*')
                ->join('cert_use use','use.id=list.useid')
                ->where(['use.comid'=>$this->userinfo['comid'],'use.deleted_user'=>0])
                ->where(['use.status'=>1,'use.isturn'=>1])
                ->where(['use.enginid'=>$enginid,'use.isreal'=>1])
                ->order('list.id ASC')
                ->select()->toArray();
            foreach ($list as $vvv){
                $vvv['months'] = (new ContractService())->get_cert_use_months($vvv['startdt'],$vvv['enddt']);
                $certfee += round($vvv['usefee']*$vvv['months'], 2);
            }

            $fee = round($data['feival'] * $certfee, 2);
        }
        return $fee;
    }
    function get_nei_months($date1,$date2){
        if(!$date2){
            $date2 = date("Y-m");
        }
        if($date1 > $date2 || !$date1){
            return 0;
        }
        $date1 = date("Y-m-d",strtotime($date1.'-01'));
        $date2 = date("Y-m-d",strtotime($date2.'-01'));
        if(strtotime($date1)>strtotime($date2)){
            $tmp=$date2;
            $date2=$date1;
            $date1=$tmp;
        }
        list($Y1,$m1,$d1)=explode('-',$date1);
        list($Y2,$m2,$d2)=explode('-',$date2);
        $y=$Y2-$Y1;
        $m=$m2-$m1;
        $d=$d2-$d1;
        if($d<0){
            $d+=(int)date('t',strtotime("-1 month $date2"));
            $m--;
        }
        if($m<0){
            $m+=12;
            $y--;
        }
        $mounth = $y*12+$m+1;
        return $mounth;
    }
    function get_cert_use_months($date1,$date2){
        if(!$date2 || $date2 == '0000-00-00'){
            $date2 = date("Y-m-d");
        }
        if($date1 > $date2 || !$date1 || $date1 == '0000-00-00'){
            return 0;
        }

        $time1 = strtotime($date1);
        $time2 = strtotime($date2);

        $days = (($time2 - $time1) / 86400) / 31;
        $days_ff = floor($days)+0.5;
        $days_cc = ceil($days);

        $days = $days < $days_ff ? $days_ff : $days_cc;
        return $days;
    }

    public function get_contract_neihetype_name($hetype=0){
        $str = '';
        if($hetype == 1){
            $str = '年度合作协议';
        }
        if($hetype == 2){
            $str = '单项目合作协议';
        }
        return $str;
    }


    public function get_contract_shi_state(){
        return [
            0=>'未签订',
            1=>'已签订',
        ];
    }
    public function get_contract_shi_zhilevel(){
        return [
            '合格',
            '优良',
        ];
    }
    public function get_contract_shi_state_name($contractid, $state){
        $name = '';
        if($state == 0){
            $name = '未签订';
        }
        if($state == 1){
            $name = '已签订';
        }
        return $name;
    }
    public function get_contract_shi_state_name_tag($contractid, $state){
        $name = '';
        if($state == 0){
            $name = '<span class="mindiancheng"></span>未签订';
        }
        if($state == 1){
            $name = '<span class="mindianlv"></span>已签订';
        }
        return $name;
    }


    public function get_contract_cai_state(){
        return [
            0=>'未签订',
            1=>'已签订',
        ];
    }
    public function get_contract_cai_state_name($contractid, $state){
        $name = '';
        if($state == 0){
            $name = '未签订';
        }
        if($state == 1){
            $name = '已签订';
        }
        return $name;
    }
    public function get_contract_cai_state_name_tag($contractid, $state){
        $name = '';
        if($state == 0){
            $name = '<span class="mindiancheng"></span>未签订';
        }
        if($state == 1){
            $name = '<span class="mindianlv"></span>已签订';
        }
        return $name;
    }
    public function get_contract_caihetype(){
        return [
            1=>'材料',
            2=>'人工',
            3=>'租赁',
            4=>'分包',
            5=>'其他',
        ];
    }
    public function get_contract_caihetype_name($hetype=0){
        $arr = $this->get_contract_caihetype();
        $str = isset($arr[$hetype]) ? $arr[$hetype] : '';
        return $str;
    }


    public function get_contract_yeararr(){
        $arr = [];
        for ($i=date("Y"); $i>=date("Y")-9; $i--){
            $arr[] = $i;
        }
        return $arr;
    }


    public function create_contract_bian_number(){
        $tou = 'CGHTBG';
        $daystr = substr(date("Ymd"), 2);
        $number = ContractBianModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }


    public function create_contract_shi_number(){
        $tou = 'SGHT';
        $daystr = substr(date("Ymd"), 2);
        $number = ContractShiModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }

    public function create_contract_bu_number(){
        $tou = 'SGHTBC';
        $daystr = substr(date("Ymd"), 2);
        $number = ContractBuModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }


    public function create_contract_cai_number(){
        $tou = 'CGHT';
        $daystr = substr(date("Ymd"), 2);
        $number = ContractCaiModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }
    public function create_contract_nei_number(){
        $tou = 'NBXY';
        $daystr = substr(date("Ymd"), 2);
        $number = ContractNeiModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }


}
